package controllers;

import models.User;
import play.Play;
import play.libs.Crypto;
import utils.ArcadiaConstants;

public class Security extends Secure.Security {
	
    static boolean authenticate(String username, String password) {
    	return User.connect(username, Crypto.encryptAES(password, Play.configuration.getProperty("application.privateKey"))) != null;
    }
    
    static void onAuthenticated() {
    	boolean isAdmin = check(ArcadiaConstants.Roles.ADMIN_ROLE);
		if(isAdmin)
			Admin.index();
		else
			Application.index();
	}
    
    static boolean check(String profile) {
		if (getConnected()!=null && getConnected().admin)
			return true;
		if ("Administrator".equals(profile)&&getConnected().admin)
			return true;
		if ("Ejecutivo".equals(profile)&&getConnected().ejecutivo)
			return true;
		if ("Visitante".equals(profile)&&getConnected().visitante)
			return true;
		
	    return false;
	}
    
    static void onDisconnected() {
		Application.index();
	}
    
    
    public static User getConnected(){
		return User.find("byUsername", connected()).<User>first();
	}
    
    public static void main(String[] args) {
    	 String password= Crypto.encryptAES("admin", "www.remax.futuro");
    	 System.out.println(password);
	}
}